import { GoogleGenAI } from "@google/genai";

const API_KEY = process.env.API_KEY;

if (!API_KEY) {
  throw new Error("API_KEY environment variable not set");
}

const ai = new GoogleGenAI({ apiKey: API_KEY });

export const generateJobDescriptionStream = async (position: string, onChunk: (text: string) => void) => {
  try {
    const prompt = `为“${position}”职位创建一份专业的职位描述。内容应包括主要职责、任职资格和所需技能。文案风格应具有吸引力且信息丰富，以吸引潜在候选人。`;

    const response = await ai.models.generateContentStream({
       model: "gemini-2.5-flash",
       contents: prompt,
    });

    for await (const chunk of response) {
      onChunk(chunk.text);
    }

  } catch (error) {
    console.error("Error generating job description:", error);
    onChunk("\n\n--- 生成描述时发生错误，请重试。 ---");
  }
};
